
//
// 110515 - AH - (Garage) Created.
// 110626 - AH - Missing java doc.
// 110805 - AH - Deprecated, plan is to replace with 
//               org.soekrat.gui.swing.chat.objectconversation.forest.
//

package org.soekrat.gui.swing.chat.objectconversation;

import java.awt.BorderLayout;

import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;

import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.forest.Tree;
import org.soekrat.gui.swing.util.AbstractTreeModel;

/**
 * <p>
 *   {@link ConversationComponent} to show 
 *   {@link Tree} retrieved.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
@Deprecated
@SuppressWarnings("serial")
public final class ForestConversationComponent extends 
  ConversationComponent 
{
  private final JTree m_tree;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param se   {@link Soekrat2Engine}.
   * @param tree {@link Tree} to show.
   */
  public ForestConversationComponent(Soekrat2Engine se, Tree tree)
  {
    super(se, "forest");
    
    if (tree==null)
    {
      throw new NullPointerException("tree");
    }
    
    setLayout(new BorderLayout());
    
    m_tree=new JTree(new TreeModel(tree));
    m_tree.setShowsRootHandles(true);
    m_tree.setRootVisible(false);
    
    m_tree.getSelectionModel().addTreeSelectionListener
    (
      new TreeSelectionListener() 
      {
        public void valueChanged(TreeSelectionEvent e) 
        {
          TreePath tp=m_tree.getSelectionPath();
          if (tp!=null && tp.getPathCount()>0)
          {
            Tree selected=(Tree)tp.getLastPathComponent();
            
            String name=selected.getName();
            
            showContent(name);
          }      
        }
      }
    );
    
    add(new JScrollPane(m_tree), BorderLayout.CENTER);
  }
  
  private static class TreeModel extends AbstractTreeModel
  {
    private Tree m_root=null;
    
    TreeModel(Tree root){ m_root=root; }

    public Object getRoot(){ return m_root; }
    
    public Object getChild(Object parent, int idx)
    {
      if (parent instanceof Tree)
      {
        return ((Tree)parent).getChild(idx);
      }
      else
      {
        throw new IndexOutOfBoundsException(Integer.toString(idx));
      }
    }
    
    public int getChildCount(Object parent)
    {
      if (parent instanceof Tree)
      {
        return ((Tree)parent).getChildCount();
      }
      else
      {
        return 0;
      }
    }

    public int getIndexOfChild(Object parent, Object child)
    {
      if (parent instanceof Tree)
      {
        return ((Tree)parent).getIndexOfChild(child);
      }
      else
      {
        return 0;
      }
    }

    public boolean isLeaf(Object node){ return getChildCount(node)==0; }
    
  }  
}
